﻿using System;
using System.IO;
using System.Windows.Forms;

namespace cndp.View
{
    public partial class FormSaveAndReadContestAndContestListToAndFromFile : Form
    {
        public string FileName;
        public string Type;

        public FormSaveAndReadContestAndContestListToAndFromFile()
        {
            FileName = "";
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            var curfile = textBoxFileName.Text + ".xml";
            if (File.Exists(curfile) && Type == "READ")
            {
                FileName = textBoxFileName.Text;
                DialogResult = DialogResult.OK;
            }
            else if (Type == "READ" && !File.Exists(curfile))
                MessageBox.Show(@"Filen hittades inte. ", @"Fel vid läsning av fil.");
            else if (Type == "SAVE" && textBoxFileName.Text != "")
            {
                FileName = textBoxFileName.Text;
                DialogResult = DialogResult.OK;
            }
            else if (Type == "SAVE" && textBoxFileName.Text == "")
                MessageBox.Show(@"Skriv in ett giltigt filnamn.", @"Fel vid skrivning till fil.");
            else
                MessageBox.Show(@"Fel vid läsning/skrivning av/till fil.", @"Error.");
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void FormSaveAndReadContestAndContestListToAndFromFile_Load(object sender, EventArgs e)
        {
            KeyPreview = true;
        }

        private void FormSaveAndReadContestAndContestListToAndFromFile_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                DialogResult = DialogResult.Cancel;
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Enter)
                buttonOK.PerformClick();
        }
    }
}